package com.car.service.money.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.car.dal.dao.client.TgClientDao;
import com.car.dal.dao.moneys.TgMoneyDao;
import com.car.dal.dao.reception.TgReceptionDao;
import com.car.dal.object.TgMoney;
import com.car.service.money.MoneyService;
import com.car.util.DateUtil;
import com.car.util.Page;
import com.car.util.StringUtil;

public class MoneyServiceImpl implements MoneyService {

	@Resource
	private TgReceptionDao receptionDao;
	@Resource
	private TgClientDao clientDao;
	@Resource
	private TgMoneyDao moneyDao;

	// 咨询老师新增总金额
	@Override
	public float getZonXzMoneys(TgMoney mon, TgMoney mon2, Date start, Date end) {
		try {
			float sum = 0;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			List<String> slist = new ArrayList<String>();

			mon.setMoneyType1(1);
			mon2.setMoneyType1(1);
			mon.setStartTime(sdf.format(start));
			mon.setEndTime(sdf.format(end));
			mon2.setEndTime(sdf.format(end));
			// 查出本月所有缴费的学生名单
			List<TgMoney> mlist = moneyDao.getMoneysStudentByType(mon);
			if (mlist != null && mlist.size() > 0) {
				for (TgMoney m : mlist) {
					if (m.getStu_name() != null && !"".equals(m.getStu_name())) {
						slist.add(m.getStu_name());
					}
					if (m.getStudent() != null && !"".equals(m.getStudent())) {
						slist.add(m.getStudent());
					}

				}
			}
			// 除去重复，拿到学生名字
			slist = StringUtil.getStringListQccf(slist);
			// 根据每个学生来查找缴费记录，然后进行判断
			if (slist != null && slist.size() > 0) {
				for (String s : slist) {
					mon2.setStudent(s);
					List<TgMoney> smlist = moneyDao.getMoneysStaticByType(mon2);
					if (smlist != null && smlist.size() > 0) {
						if (smlist.size() == 1) {// 一条记录就判断是否是定金
							if (smlist.get(0).getMoneyType2() != 1) {
								sum += smlist.get(0).getMoneys();
							}
						} else {
							if (smlist.get(1).getMoneyType2() == 4 || smlist.get(1).getMoneyType2() == 3) {// 第二次交尾款或全款
								for (TgMoney m : smlist) {
									if (DateUtil.compareDate(m.getMoneyDate(), mon.getStartTime())) {
										sum += m.getMoneys();
									}
								}
							} else if (smlist.get(1).getMoneyType2() == 2) {// 第二次交部分款
								Long mdate = sdf.parse(smlist.get(1).getMoneyDate()).getTime();

								if (mdate >= start.getTime() && mdate <= end.getTime()) {
									for (TgMoney m : smlist) {
										sum += m.getMoneys();
									}
								} else {
									for (TgMoney m : smlist) {
										Long mdatem = sdf.parse(m.getMoneyDate()).getTime();
										if (mdatem >= start.getTime() && mdatem <= end.getTime()) {
											sum += m.getMoneys();
										}
									}
								}
							} else {
								for (TgMoney m : smlist) {
									if (DateUtil.compareDate(m.getMoneyDate(), mon.getStartTime())) {
										sum += m.getMoneys();
									}
								}
							}
						}
					}
				}
			}
			return sum;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;

	}

	// 咨询老师成单总数
	// 成单数是按照第一次缴费算的，只要是第一次缴费的，除了夏令营的就算是成单了，不是第一次的就不算成单
	// 这个就是全款和定金都算成单，部分款的话就看这一周之前有没有交过款，有的话不算成单，没有交过就算成单。
	@Override
	public Integer getZonCdByMoneys(TgMoney mon, TgMoney mon2, Date start, Date end) {
		try {
			Integer sum = 0;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			List<String> slist = new ArrayList<String>();

			mon.setMoneyType1(1);
			mon2.setMoneyType1(1);
			mon.setStartTime(sdf.format(start));
			mon.setEndTime(sdf.format(end));
			mon2.setEndTime(sdf.format(end));
			// 查出本月所有缴费的学生名单
			List<TgMoney> mlist = moneyDao.getMoneysStudentByType(mon);
			if (mlist != null && mlist.size() > 0) {
				for (TgMoney m : mlist) {
					if (m.getStu_name() != null && !"".equals(m.getStu_name())) {
						slist.add(m.getStu_id() + "");
					}
					if (m.getStudent() != null && !"".equals(m.getStudent())) {
						slist.add(m.getStu_id() + "");
					}

				}
			}
			// 除去重复，拿到学生名字
			slist = StringUtil.getStringListQccf(slist);
			// 根据每个学生来查找缴费记录，然后进行判断
			if (slist != null && slist.size() > 0) {
				for (String s : slist) {
					if(s.equals("134302")){
						System.out.println("daol");
					}
					mon2.setStu_id(Long.parseLong(s));
					List<TgMoney> smlist = moneyDao.getMoneysStaticByType(mon2);
					if (smlist != null && smlist.size() > 0) {
						Long mdate = sdf.parse(smlist.get(0).getMoneyDate()).getTime();
						// 这个就是全款和定金都算成单，部分款的话就看这一周之前有没有交过款，有的话不算成单，没有交过就算成单。
						if (mdate >= start.getTime() && mdate <= end.getTime() && smlist.get(0).getMoneyType2() != 4) {
							sum++;
						}
					}
				}
			}
			return sum;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;

	}

	// 获取excel招生记录按中心 交款时间优先级排序
	@Override
	public List<TgMoney> getEnrollmentRecordForExcel(TgMoney mon, Page page) {
		// TODO Auto-generated method stub
		Comparator<TgMoney> compareCenterDESC = new Comparator<TgMoney>() {

			@Override
			public int compare(TgMoney o1, TgMoney o2) {

				if (o1.getJd_center_key().compareTo(o2.getJd_center_key()) > 0) {
					return 1;
				} else if (o1.getJd_center_key().compareTo(o2.getJd_center_key()) < 0) {
					return -1;
				}

				return 0;

			}
		};

		Comparator<TgMoney> compareDateDESC = new Comparator<TgMoney>() {

			@Override
			public int compare(TgMoney o1, TgMoney o2) {
				if (o1.getMoneyDate().compareTo(o2.getMoneyDate()) > 0) {
					return -1;
				} else if (o1.getMoneyDate().compareTo(o2.getMoneyDate()) < 0) {
					return 1;
				}

				return 0;

			}

		};
		List<TgMoney> list = moneyDao.getEnrollmentRecord(mon, page);
		final List<Comparator<TgMoney>> mCmpList = new ArrayList<Comparator<TgMoney>>();
		mCmpList.add(compareCenterDESC);
		mCmpList.add(compareDateDESC);

		Comparator<TgMoney> cmp = new Comparator<TgMoney>() {
			@Override
			public int compare(TgMoney o1, TgMoney o2) {
				for (Comparator<TgMoney> comparator : mCmpList) {
					if (comparator.compare(o1, o2) > 0) {
						return 1;
					} else if (comparator.compare(o1, o2) < 0) {
						return -1;
					}
				}
				return 0;
			}
		};
		Collections.sort(list, cmp);

		return list;
	}

}
